package com.tongdainfo.common.parser;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.PDFTextStripper;

public class PdfParser implements IParser {

    private String[] fileTypes = {"pdf"};

    public String[] getFileTypes() {
        return fileTypes;
    }

	public String readText(File file, String charset) throws Exception {
		String text = "";
        PDDocument document = null;

        document = PDDocument.load(file);
        PDFTextStripper stripper = new PDFTextStripper();
        text = stripper.getText(document);

        if (document != null) {
            document.close();

        }
        return text;
	}

	@Override
	public String readInput(InputStream input, String charset) throws Exception {
		String text = "";
        PDDocument document = null;

//        document = PDDocument.load(file);
        document = PDDocument.load(input);
        
        PDFTextStripper stripper = new PDFTextStripper();
        text = stripper.getText(document);

        if (document != null) {
            document.close();

        }
        return text;
	}

   /* public String readText(File file, String charsetName) throws IOException {

        String text = "";
        PDDocument document = null;

        document = PDDocument.load(file);
        PDFTextStripper stripper = new PDFTextStripper();
        text = stripper.getText(document);

        if (document != null) {
            document.close();

        }
        return text;
    }*/

  /*  public static void main(String[] s) {
        String result = "";
        try {
            result = new PdfParser().readText(new File("./c.pdf"), "GBK");
        } catch (IOException ex) {
            Logger.getLogger(PdfParser.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println(result);
    }*/
}
